<?php
declare(strict_types=1);

/**
 * @user magein
 * @date 2024/1/18 17:30
 */

namespace seecms\controller;

use seecms\See;
use seecms\lib\Backup;
use seecms\lib\Mailer;
use seecms\lib\Operate;
use seecms\lib\Transfer;
use seecms\SeeLang;
use seecms\lib\builder\PageParse;
use seecms\lib\response\FileResponse;
use seecms\lib\response\HtmlResponse;
use seecms\lib\response\JsonResponse;


class Database
{
    /**
     * @rule_title 数据库备份
     * @return string
     */
    public function setting(): string
    {
        $tables = See::config()->backup->tables();

        $setting = See::provider()->general->backup;

        return See::view()->fetch('database/index', [
            'tables' => $tables,
            'setting' => $setting,
            'zip' => extension_loaded('zip'),
            'queries' => PageParse::global('database')->query(['name', 'created_at']),
        ]);
    }

    /**
     * @rule_title 备份日志
     */
    public function index()
    {
        $where = Transfer::pageQuery(['name', 'created_at'], See::request()->get());
        $data = See::table()->database_backup->where($where)->paginate();
        $list = $data['data'];
        foreach ($list as $key => $item) {
            $item['filename'] = pathinfo($item['filepath'], PATHINFO_BASENAME);
            $item['filesize_text'] = Transfer::filesizeFormatter($item['filesize']);
            $item['send_result_text'] = Backup::translateSendResult($item['send_result']);
            $list[$key] = $item;
        }
        $data['data'] = $list;

        return JsonResponse::success($data);
    }

    /**
     * @rule_title 备份下载
     */
    public function download()
    {
        $id = See::request()->get('id');

        if (empty($id)) {
            return new HtmlResponse(Operate::required());
        }

        $record = See::table()->database_backup->find($id);

        if (empty($record)) {
            return new HtmlResponse(Operate::notFoundData());
        }

        $root_path = See::config()->backup->root();
        $filepath = $root_path . '/' . $record['filepath'];

        if (is_file($filepath)) {
            return (new FileResponse($filepath))->name($filepath);
        }

        return new HtmlResponse(SeeLang::general('not_found_file'));
    }

    /**
     * @rule_title 执行备份
     * @return JsonResponse
     */
    public function backup(): JsonResponse
    {
        $tables = See::request()->post('tables');

        if (empty($tables)) {
            return JsonResponse::error(Operate::required('tables'));
        }

        $backup = new Backup();
        $backup->setTables($tables);
        $backup->setMailer(new Mailer());
        $result = $backup->run();

        return JsonResponse::result($result);
    }

    /**
     * @rule_title 备份发送到邮箱
     * @return JsonResponse
     */
    public function send(): JsonResponse
    {
        $id = See::request()->post('id');

        if (empty($id)) {
            return JsonResponse::error(Operate::required());
        }

        $record = See::table()->database_backup->find($id);

        if (empty($record)) {
            return JsonResponse::error(Operate::notFoundData());
        }

        $backup = See::provider()->general->backup;

        $receive_email = $backup['receive_email'];
        if (empty($receive_email)) {
            return JsonResponse::error(Operate::required('receive_email'));
        }
        $receive_email = explode(',', $receive_email);

        $backup = new Backup();
        $backup->setReceiveEmail($receive_email);
        $backup->setMailer(new Mailer(See::provider()->general->smtp));
        $result = $backup->sendMail($record);

        return JsonResponse::result($result);
    }
}